package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.dao.CellSluggishMaterialsDao;
import com.wing.cabinet.model.entity.AuthorityMember;
import com.wing.cabinet.model.entity.CellMaterialTimezoneSetting;
import com.wing.cabinet.model.entity.CellSluggishMaterials;
import com.wing.cabinet.model.request.CellSluggishMaterialsForm;
import com.wing.cabinet.model.response.CellInventoryProductVO;
import com.wing.cabinet.model.response.CellSluggishMaterialsVO;
import com.wing.cabinet.utils.FindDateStatusUtil;
import com.wing.common.utils.JsonResult;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.model.entity.ProductType;
import com.wing.product.model.response.ProductTypeVO;
import com.wing.product.service.ProductDetailService;
import com.wing.product.service.ProductTypeService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 呆滞物料管理-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CellSluggishMaterialsServiceImpl extends ServiceImpl<CellSluggishMaterialsDao, CellSluggishMaterials> implements CellSluggishMaterialsService {

    @Autowired
    private CellInventoryProductService cellInventoryProductService;

    @Autowired
    private CellMaterialTimezoneSettingService cellMaterialTimezoneSettingService;

    @Autowired
    private ProductDetailService productDetailService;

    @Autowired
    private ProductTypeService productTypeService;

    @Override
    public int add(CellSluggishMaterialsForm cellSluggishMaterialsForm) {

        //校验

        CellSluggishMaterials cellSluggishMaterials =new CellSluggishMaterials();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CellSluggishMaterials.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(cellSluggishMaterialsForm, cellSluggishMaterials, copyOptions);

        return baseMapper.insert(cellSluggishMaterials);
    }

    @Override
    public List<CellSluggishMaterialsVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CellSluggishMaterialsVO> pageList4app(Page page, Map<String, Object> params) {
        List<CellSluggishMaterialsVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CellSluggishMaterialsVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public JsonResult addCellSluggishMaterials() {
        Map queryParam=new HashMap();
        queryParam.put("enterpriseCode",JwtUtils.getEnterpriseCode());
        List<CellInventoryProductVO> list = cellInventoryProductService.list4app(queryParam);
        for(CellInventoryProductVO item:list){
            if(StringUtils.isNotBlank(item.getStartDay()) && StringUtils.isNotBlank(item.getEndDay())){
                int startDay = Integer.parseInt(item.getStartDay());
                int endDay = Integer.parseInt(item.getEndDay());
                if(StringUtils.isNotBlank(item.getDays())){
                    if(item.getUpdateTime()!=null){
                        int days = Integer.parseInt(item.getDays());
                        if(rangeInDefined(days,startDay,endDay)){
                            CellSluggishMaterials cellSluggishMaterials = new CellSluggishMaterials();
                            cellSluggishMaterials.setSkuCode(item.getSkuCode());
                            cellSluggishMaterials.setExhibitTime(item.getCreateTime());
                            cellSluggishMaterials.setReceiptorTime(item.getUpdateTime());
                            baseMapper.insert(cellSluggishMaterials);
                        }
                    }else {
                        String creatTime = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(item.getCreateTime());
                        String todayTime = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now());
                        if(rangeInDefined(FindDateStatusUtil.daysBetween(creatTime,todayTime),startDay,endDay)){
                            CellSluggishMaterials cellSluggishMaterials = new CellSluggishMaterials();
                            cellSluggishMaterials.setSkuCode(item.getSkuCode());
                            cellSluggishMaterials.setExhibitTime(item.getCreateTime());
                            cellSluggishMaterials.setReceiptorTime(item.getUpdateTime());
                            baseMapper.insert(cellSluggishMaterials);
                        }
                    }
                }
            }
        }
        return JsonResult.success("添加成功");
    }

    @Override
    public Page<CellSluggishMaterials> pageList(Page page, Map<String, Object> params) {
        int settingDay = 0;
        int differDay = 0;
        List<CellSluggishMaterials> cellSluggishMaterialsList =  new ArrayList<>();
        Map queryParam=new HashMap();
        queryParam.put("enterpriseCode",JwtUtils.getEnterpriseCode());
        List<CellInventoryProductVO> list = cellInventoryProductService.list4app(queryParam);
        for(CellInventoryProductVO item:list){
            ProductDetail productDetail = productDetailService.getOne(new LambdaQueryWrapper<ProductDetail>().eq(ProductDetail::getSkuCode, item.getSkuCode()));
            if(productDetail != null){
                ProductTypeVO productTypeVO = productTypeService.get4appById(productDetail.getProductTypeId());
                // 优先取设置了呆滞归还时长的
                if(productDetail.getLethargyThreshold()!=null){
                    settingDay = productDetail.getLethargyThreshold();
                    if(StringUtils.isNotBlank(item.getDays())){
                        differDay = Integer.parseInt(item.getDays());
                    }
                    if(differDay > settingDay){
                        CellSluggishMaterials cellSluggishMaterials = new CellSluggishMaterials();
                        cellSluggishMaterials.setSkuCode(item.getSkuCode());
                        cellSluggishMaterials.setExhibitTime(item.getCreateTime());
                        cellSluggishMaterials.setReceiptorTime(item.getUpdateTime());
                        cellSluggishMaterials.setSkuName(item.getSkuName());
                        cellSluggishMaterials.setSkuSpec(item.getSkuSpec());
                        cellSluggishMaterials.setProductBrandName(item.getProductBrandName());
                        cellSluggishMaterials.setQuantityPack(item.getQuantity());
                        cellSluggishMaterials.setQuantity(item.getQuantityPack());
                        cellSluggishMaterials.setSkuPic(productDetail.getProductPic());
                        cellSluggishMaterials.setProductTypeName(item.getProductTypeName());
                        cellSluggishMaterials.setOldSpecCode(item.getSkuSpecOld());
                        cellSluggishMaterials.setUnitName(item.getUnitName());
                        cellSluggishMaterials.setBasicsUnitName(item.getBasicsUnitName());
                        cellSluggishMaterials.setLethargyThreshold(settingDay);
                        cellSluggishMaterialsList.add(cellSluggishMaterials);
                    }
                }else if(productTypeVO != null && StringUtils.isNotBlank(productTypeVO.getDays())){
                    if(StringUtils.isNotBlank(item.getDays())){
                        settingDay = Integer.parseInt(productTypeVO.getDays());
                        differDay = Integer.parseInt(item.getDays());
                        if(item.getUpdateTime()!=null){
                            if(differDay > settingDay){
                                CellSluggishMaterials cellSluggishMaterials = new CellSluggishMaterials();
                                cellSluggishMaterials.setSkuCode(item.getSkuCode());
                                cellSluggishMaterials.setExhibitTime(item.getCreateTime());
                                cellSluggishMaterials.setReceiptorTime(item.getUpdateTime());
                                cellSluggishMaterials.setSkuName(item.getSkuName());
                                cellSluggishMaterials.setSkuSpec(item.getSkuSpec());
                                cellSluggishMaterials.setProductBrandName(item.getProductBrandName());
                                cellSluggishMaterials.setQuantityPack(item.getQuantity());
                                cellSluggishMaterials.setQuantity(item.getQuantity());
                                cellSluggishMaterials.setSkuPic(productDetail.getProductPic());
                                cellSluggishMaterials.setProductTypeName(item.getProductTypeName());
                                cellSluggishMaterials.setOldSpecCode(item.getSkuSpecOld());
                                cellSluggishMaterials.setUnitName(item.getUnitName());
                                cellSluggishMaterials.setBasicsUnitName(item.getBasicsUnitName());
                                cellSluggishMaterials.setLethargyThreshold(settingDay);
                                cellSluggishMaterialsList.add(cellSluggishMaterials);
                            }
                        }else {
                            String creatTime = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(item.getCreateTime());
                            String todayTime = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now());
                            if(FindDateStatusUtil.daysBetween(creatTime,todayTime) > settingDay){
                                CellSluggishMaterials cellSluggishMaterials = new CellSluggishMaterials();
                                cellSluggishMaterials.setSkuCode(item.getSkuCode());
                                cellSluggishMaterials.setSkuName(item.getSkuName());
                                cellSluggishMaterials.setSkuSpec(item.getSkuSpec());
                                cellSluggishMaterials.setProductBrandName(item.getProductBrandName());
                                cellSluggishMaterials.setExhibitTime(item.getCreateTime());
                                cellSluggishMaterials.setReceiptorTime(item.getUpdateTime());
                                cellSluggishMaterials.setQuantityPack(item.getQuantity());
                                cellSluggishMaterials.setQuantity(item.getQuantity());
                                cellSluggishMaterials.setSkuPic(productDetail.getProductPic());
                                cellSluggishMaterials.setProductTypeName(item.getProductTypeName());
                                cellSluggishMaterials.setOldSpecCode(item.getSkuSpecOld());
                                cellSluggishMaterials.setUnitName(item.getUnitName());
                                cellSluggishMaterials.setBasicsUnitName(item.getBasicsUnitName());
                                cellSluggishMaterials.setLethargyThreshold(settingDay);
                                cellSluggishMaterialsList.add(cellSluggishMaterials);
                            }

                        }
                    }
                }
            }
        }
        page.setRecords(cellSluggishMaterialsList);
        return page;
    }

    public static boolean rangeInDefined(int current, int min, int max) {
        return Math.max(min, current) == Math.min(current, max);
    }

}
